# !/usr/bin/env python
# encoding: utf-8
import time
from ATS.application.LASER_App.N7744C_App import N7744C_App
from ATS.application.LASER_App.L8164B_App import L8164B_App
import csv
import numpy as np
from matplotlib import pyplot as plt



def get_step_array(start, stop, step):
    array_list = np.arange(start, stop, step)
    array_list = [str(i) for i in array_list]
    array_list.append(str(stop))
    return array_list


def plot_curve(x_data, y_data):
    plt.plot(x_data, y_data)
    plt.xlabel("WaveLength")
    plt.ylabel("Power")
    plt.grid(True)
    plt.show()


def spectral_scan():
    n7744c_app = N7744C_App("SS2")
    l8164b_app = L8164B_App("SS3")
    n7744c_app.set_logging_state("STOP")
    x_data = get_step_array(1260, 1360, 1)
    points = len(x_data)
    l8164b_app.set_power_unit()
    l8164b_app.set_power_value(10)
    time.sleep(3)
    p_in = l8164b_app.fetch_input_power()
    l8164b_app.set_sweep_mode()
    l8164b_app.set_repeat_mode()
    l8164b_app.set_wave_sweep_speed()
    l8164b_app.set_start_stop_step_wave(1260, 1360, 1)
    l8164b_app.trigger_config()
    l8164b_app.trigger_output()
    l8164b_app.trigger_input()
    l8164b_app.output_on()
    l8164b_app.set_wave_sweep_state()
    # n7744c_app.reset()
    n7744c_app.set_power_unit()
    n7744c_app.set_power_range(10)
    n7744c_app.set_auto_gain()
    n7744c_app.set_power_atim()
    n7744c_app.trigger_input_mode()
    n7744c_app.set_power_wave_length()
    n7744c_app.set_logging_points(points)
    n7744c_app.set_logging_state()
    n7744c_app.tigger_status()
    while True:
        state = n7744c_app.get_func_state()
        if state == "LOGGING_STABILITY,COMPLETE":
            y_data = n7744c_app.get_func_res()
            break
    y_data = [float(i) for i in y_data]
    l8164b_app.stop_sweep()


spectral_scan()
